<template>
  <div class="attestation">
    <div class="headline">认证类型</div>
    <el-radio-group v-model="attestationType" @change="tabChange">
      <el-radio-button label="个人认证" />
      <el-radio-button label="企业认证" />
      <el-radio-button label="其他机构认证" />
    </el-radio-group>
    <div class="attestation-hint">请填写认证信息并上传认证材料后提交申请，1个工作日内完成审核。</div>
    <div class="headline">主体信息</div>
    <el-form ref="personForm" :model="personForm" :rules="personRules" label-width="140px">
      <div v-if="attestationType== '个人认证'">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="personForm.name" style="width: 50%" />
        </el-form-item>
        <el-form-item label="证件类型">
          <span>中国大陆居民身份证</span>
          <el-tooltip content="目前仅支持使用中国大陆居民身份证进行个人认证" placement="right" effect="light">
            <i class="el-icon-info" />
          </el-tooltip>
        </el-form-item>
        <el-form-item label="身份证号码" prop="idNum">
          <el-input v-model="personForm.idNum" style="width: 50%" />
        </el-form-item>
        <el-form-item label="身份证照片" prop="frontImg">
          <div class="idNum-box">
            <el-upload
              :show-file-list="false"
              :on-success="idNumFrontSuccess"
              :before-upload="beforUpload"
              class="avatar-uploader"
              action="https://jsonplaceholder.typicode.com/posts/"
            >
              <img v-if="personForm.frontImg" :src="personForm.frontImg" class="avatar" />
              <div v-else class="hintText">点击上传身份证(人像面)</div>
              <div v-if="personForm.frontImg" class="el-upload-list__item-actions">重新上传</div>
            </el-upload>

            <el-upload
              :show-file-list="false"
              :on-success="idNumBackSuccess"
              :before-upload="beforUpload"
              class="avatar-uploader"
              action="https://jsonplaceholder.typicode.com/posts/"
            >
              <img v-if="personForm.backImg" :src="personForm.backImg" class="avatar" />
              <div v-else class="hintText">点击上传身份证(国徽面)</div>
              <div v-if="personForm.backImg" class="el-upload-list__item-actions">重新上传</div>
            </el-upload>
          </div>
        </el-form-item>
        <el-form-item label="手持证件照" prop="handImg">
          <div class="idNum-box">
            <el-upload
              :show-file-list="false"
              :on-success="idNumHandSuccess"
              :before-upload="beforUpload"
              class="avatar-uploader"
              action="https://jsonplaceholder.typicode.com/posts/"
            >
              <img v-if="personForm.handImg" :src="personForm.handImg" class="avatar" />
              <div v-else class="hintText">点击上传手持证件照</div>
              <div v-if="personForm.handImg" class="el-upload-list__item-actions">重新上传</div>
            </el-upload>
            <div class="reference" @click="referenceDialog = true">
              <img src="/src/assets/analyze_data/2018710sfsl.jpg" alt />
              <div class="reference-tag">参考图示</div>
            </div>
            <el-dialog :visible.sync="referenceDialog">
              <img width="100%" src="/src/assets/analyze_data/2018710sfsl.jpg" alt />
            </el-dialog>
          </div>
        </el-form-item>
      </div>
      <div v-if="attestationType=='企业认证'">
        <el-form-item label="企业名称" prop="orgName">
          <el-input v-model="personForm.orgName" style="width: 50%" />
        </el-form-item>
        <el-form-item label="法人" prop="name">
          <el-input v-model="personForm.name" style="width: 50%" />
        </el-form-item>
        <el-form-item label="法人身份证号码" prop="idNum">
          <el-input v-model="personForm.idNum" style="width: 50%" />
        </el-form-item>
        <el-form-item label="法人身份证照片" prop="frontImg">
          <div class="idNum-box">
            <el-upload
              :show-file-list="false"
              :on-success="idNumFrontSuccess"
              :before-upload="beforUpload"
              class="avatar-uploader"
              action="https://jsonplaceholder.typicode.com/posts/"
            >
              <img v-if="personForm.frontImg" :src="personForm.frontImg" class="avatar" />
              <div v-else class="hintText">点击上传身份证(人像面)</div>
              <div v-if="personForm.frontImg" class="el-upload-list__item-actions">重新上传</div>
            </el-upload>

            <el-upload
              :show-file-list="false"
              :on-success="idNumBackSuccess"
              :before-upload="beforUpload"
              class="avatar-uploader"
              action="https://jsonplaceholder.typicode.com/posts/"
            >
              <img v-if="personForm.backImg" :src="personForm.backImg" class="avatar" />
              <div v-else class="hintText">点击上传身份证(国徽面)</div>
              <div v-if="personForm.backImg" class="el-upload-list__item-actions">重新上传</div>
            </el-upload>
          </div>
        </el-form-item>
        <el-form-item label="统一社会信用代码" prop="creditNum">
          <el-input v-model="personForm.creditNum" style="width: 50%" />
        </el-form-item>
        <el-form-item label="营业执照">
          <div class="corpProve">
            <div
              style="width:50%;color:font-size: 14px;color: #888888;line-height: 20px;"
            >仅支持在有效期内的中国大陆工商局或市场监督管理局颁发的工商营业执照。格式要求：原件照片、扫描件或者复印件加盖企业公章后的扫描件，支持.jpg .jpeg .bmp .gif .png格式照片，大小不超过5M</div>
            <el-upload
              class="upload-demo"
              :on-change="corpProveChange"
              action="https://jsonplaceholder.typicode.com/posts/"
              :file-list="personForm.corpProveList"
              list-type="picture"
            >
              <el-button size="small" type="primary">点击上传</el-button>
            </el-upload>
          </div>
        </el-form-item>
      </div>
      <div v-if="attestationType=='其他机构认证'">
        <el-form-item label="机构名称" prop="orgName">
          <el-input v-model="personForm.orgName" style="width: 50%" />
        </el-form-item>
        <el-form-item label="负责人" prop="name">
          <el-input v-model="personForm.name" style="width: 50%" />
        </el-form-item>
        <el-form-item label="负责人身份证号码" prop="idNum">
          <el-input v-model="personForm.idNum" style="width: 50%" />
        </el-form-item>
        <el-form-item label="负责人身份证照片" prop="frontImg">
          <div class="idNum-box">
            <el-upload
              :show-file-list="false"
              :on-success="idNumFrontSuccess"
              :before-upload="beforUpload"
              class="avatar-uploader"
              action="https://jsonplaceholder.typicode.com/posts/"
            >
              <img v-if="personForm.frontImg" :src="personForm.frontImg" class="avatar" />
              <div v-else class="hintText">点击上传身份证(人像面)</div>
              <div v-if="personForm.frontImg" class="el-upload-list__item-actions">重新上传</div>
            </el-upload>

            <el-upload
              :show-file-list="false"
              :on-success="idNumBackSuccess"
              :before-upload="beforUpload"
              class="avatar-uploader"
              action="https://jsonplaceholder.typicode.com/posts/"
            >
              <img v-if="personForm.backImg" :src="personForm.backImg" class="avatar" />
              <div v-else class="hintText">点击上传身份证(国徽面)</div>
              <div v-if="personForm.backImg" class="el-upload-list__item-actions">重新上传</div>
            </el-upload>
          </div>
        </el-form-item>
        <el-form-item label="机构证件或其他证明材料（最多5张）">
          <div class="corpProve">
            <div style="width:50%;color:font-size: 14px;color: #888888;line-height: 20px;">
              此处提交机构证件及其他证明材料。
              <br />支持.jpg .jpeg .bmp .gif .png格式照片，大小不超过5M。
            </div>
            <el-upload
              :on-change="orgProveChange"
              :file-list="personForm.orgProveList"
              class="upload-demo"
              action="https://jsonplaceholder.typicode.com/posts/"
              list-type="picture"
            >
              <el-button size="small" type="primary">点击上传</el-button>
            </el-upload>
          </div>
        </el-form-item>
      </div>
      <div class="headline">验证管理员身份</div>
      <div style="color:#353535;margin:20px 0;">验证码将发送到管理员手机号：15210964997，请注意查收</div>
      <el-form-item label="短信验证码" prop="verifyYard">
        <el-input v-model="personForm.verifyYard" style="width:300px;" />
        <el-button>获取验证码</el-button>
      </el-form-item>
      <el-button type="primary" class="submitBtn" @click="submitForm">提交认证信息</el-button>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      attestationType: '个人认证',
      personForm: {
        verifyYard: '',
        orgName: '',
        name: '',
        idNum: '',
        creditNum: '',
        frontImg: '',
        backImg: '',
        handImg: '',
        corpProveList: [],
        orgProveList: []

      },
      referenceDialog: false,

      personRules: {
        name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        creditNum: [
          { required: true, message: '请输入社会信用代码', trigger: 'blur' },
          { pattern: /^[^_IOZSVa-z\W]{2}\d{6}[^_IOZSVa-z\W]{10}$/g, message: '请输入正确的统一社会信用代码' }
        ],
        idNum: [
          { required: true, message: '请输入身份证号码', trigger: 'blur' },
          { pattern: /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/, message: '身份证格式不正确' }
        ],
        frontImg: [{ required: true, message: '请上传身份证', trigger: 'change' }],
        handImg: [{ required: true, message: '请上传手持证件照', trigger: 'blur' }],
        orgName: [{ required: true, message: '请输入企业名称', trigger: 'blur' }],
        verifyYard: [{ required: true, message: '请输入验证码', trigger: 'blur' }]
      }
    }
  },
  methods: {
    tabChange(val) {
      console.log(val)
      console.log(this.$refs.personForm)
      this.$refs.personForm.resetFields()
    },
    beforUpload(file) {
      const isJPG = file.type === 'image/jpeg' || 'image/png'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片格式不正确!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    idNumFrontSuccess(res, file) {
      this.personForm.frontImg = URL.createObjectURL(file.raw)
    },
    idNumBackSuccess(res, file) {
      this.personForm.backImg = URL.createObjectURL(file.raw)
    },
    idNumHandSuccess(res, file) {
      this.personForm.handImg = URL.createObjectURL(file.raw)
    },
    corpProveChange(file, fileList) {
      this.personForm.corpProveList = fileList.slice(-1)
    },
    orgProveChange(file, fileList) {
      this.personForm.orgProveList = fileList.slice(-5)
    },
    submitForm() {
      this.$refs.personForm.validate((valid) => {
        console.log(valid)
        if (valid) {
          this.$message({
            type: 'success',
            message: '提交成功'
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.attestation {
  padding: 20px;
  .headline {
    font-weight: bold;
    color: #353535;
    font-size: 16px;
    padding-left: 8px;
    margin: 20px 0;
    box-sizing: border-box;
    border-left: 2px solid #2a75ed;
    height: 22px;
    line-height: 22px;
  }
  .attestation-hint {
    margin-top: 10px;
    margin-bottom: 20px;
    width: 800px;
    color: #888888;
    font-size: 14px;
  }
  .idNum-box {
    width: 640px;
    display: flex;
    justify-content: space-between;
  }
  /deep/.avatar-uploader {
    cursor: pointer;
    position: relative;
    overflow: hidden;
    width: 300px;
    height: 200px;
    .hintText {
      color: RGBA(136, 136, 136, 1);
      font-size: 14px;
      width: 300px;
      height: 200px;
      line-height: 200px;
      background: #f7f8f9;
      text-align: center;
      border-radius: 5px;
    }
    .avatar {
      width: 300px;
      height: 200px;
      display: block;
    }
    .el-upload-list__item-actions {
      position: absolute;
      width: 100%;
      height: 100%;
      left: 0;
      top: 0;
      width: 300px;
      height: 200px;
      line-height: 200px;
      color: #f6f7ee;
      cursor: pointer;
      text-align: center;
      opacity: 0;
      font-size: 20px;
      background-color: rgba(0, 0, 0, 0.5);
      transition: opacity 0.3s;
    }
    .el-upload-list__item-actions:hover {
      opacity: 1;
    }
  }

  .reference {
    width: 300px;
    height: 200px;
    position: relative;
    .reference-tag {
      position: absolute;
      top: 20px;
      left: 0;
      width: 64px;
      height: 20px;
      background: #fb6161;
      border-radius: 0px 20px 20px 0px;
      font-size: 12px;
      color: #ffffff;
      line-height: 20px;
      box-sizing: border-box;
      padding-left: 5px;
    }
    img {
      width: 100%;
      height: 100%;
    }
  }
  .corpProve {
    /deep/.el-upload-list {
      width: 50%;
    }
  }
  .submitBtn {
    margin: 20px 0 40px 140px;
  }
}
</style>
